<?php
header('Content-type: text/xml');

function make_app($step) {
	$xmlstr = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<dummy-app>
<nav>$step</nav>
<app xmlns="http://www.w3.org/1999/xhtml"></app>
</dummy-app>
XML;
	return simplexml_load_string($xmlstr);
}

function echo_msg($str) {
	echo <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<dummy-msg>$str</dummy-msg>
XML;
}

function add_child($node, $child, $arr) {
	$cur = $node->addChild($child);
	foreach ($arr as $n => $v)
		$cur->addAttribute($n, $v);
	return $cur;
}

function set_session_cookie($name, $value) {
	$params = session_get_cookie_params();
	setrawcookie($name, rawurlencode($value), 
		$value ? time()+3600 : time()-42000, 
		$params['path'], $params['domain'], 
		$params['secure'], $params['httponly']);
}

function mysql_result_html($rst) {
	$n = mysql_num_fields($rst);
	$html = "<table>";
	$html.= "<tr>";
	for ($i = 0; $i < $n; $i++)
		$html.= "<th>" . mysql_field_name($rst, $i) . "</th>";
	$html.= "</tr>";
	while($row = mysql_fetch_row($rst)) {
		$html.= "<tr>";
		foreach($row as $value)
			$html.= "<td>$value</td>";
		$html.= "</tr>";
	}
	$html.= "</table>";
	return $html;
}

function db_escape($values, $quotes = true) { 
	if (is_array($values))
		foreach ($values as $key => $value)
			$values[$key] = db_escape($value, $quotes);
	else if ($values === null)
		$values = 'NULL';
	else if (is_bool($values))
		$values = $values ? 1 : 0;
	else if (!is_numeric($values)) {
		$values = mysql_real_escape_string($values);
		if ($quotes) $values = '"' . $values . '"';
	}
	return $values;
}

function session_end() {
	if (session_id != "" || isset($_COOKIE[session_name()]))
		set_session_cookie(session_name(), '');
	session_destroy();
}

function exit_404($msg = '') {
	header("Status: 404 Not Found");
	echo_msg($msg);
	exit;
}
?>
